/**
 * 几何操作工具函数
 */

/**
 * 添加新点
 */
export function addPoint(coords, geometryData) {
  const newPoint = {
    id: `point_${Date.now()}`,
    x: coords.x,
    y: coords.y,
    label: '',
    color: '#007AFF',
    draggable: true
  };

  return {
    ...geometryData,
    points: [...geometryData.points, newPoint]
  };
}

/**
 * 添加新线段
 */
export function addLine(startPoint, endPoint, geometryData) {
  const newLine = {
    id: `line_${Date.now()}`,
    startPointId: startPoint.id,
    endPointId: endPoint.id,
    style: 'solid',
    color: '#000',
    width: 2
  };

  return {
    ...geometryData,
    lines: [...geometryData.lines, newLine]
  };
}

/**
 * 删除点
 */
export function deletePoint(pointId, geometryData) {
  return {
    ...geometryData,
    points: geometryData.points.filter(p => p.id !== pointId),
    lines: geometryData.lines.filter(l =>
      l.startPointId !== pointId && l.endPointId !== pointId
    ),
    perpendiculars: geometryData.perpendiculars.filter(perp =>
      perp.fromPointId !== pointId
    ),
    parallels: geometryData.parallels.filter(para =>
      para.fromPointId !== pointId
    ),
    constants: geometryData.constants.filter(c =>
      !c.pointIds || !c.pointIds.includes(pointId)
    ),
    fills: geometryData.fills.filter(f =>
      !f.pointIds || !f.pointIds.includes(pointId)
    )
  };
}

/**
 * 删除线段
 */
export function deleteLine(lineId, geometryData) {
  return {
    ...geometryData,
    lines: geometryData.lines.filter(l => l.id !== lineId),
    perpendiculars: geometryData.perpendiculars.filter(perp =>
      !(perp.toLineStartId === geometryData.lines.find(l => l.id === lineId)?.startPointId &&
        perp.toLineEndId === geometryData.lines.find(l => l.id === lineId)?.endPointId)
    )
  };
}

/**
 * 删除垂线
 */
export function deletePerpendicular(perpId, geometryData) {
  const perp = geometryData.perpendiculars.find(p => p.id === perpId);
  if (!perp) return geometryData;

  return {
    ...geometryData,
    perpendiculars: geometryData.perpendiculars.filter(p => p.id !== perpId),
    points: geometryData.points.filter(p => p.id !== perp.footPointId)
  };
}

/**
 * 删除平行线
 */
export function deleteParallel(paraId, geometryData) {
  const para = geometryData.parallels.find(p => p.id === paraId);
  if (!para) return geometryData;

  return {
    ...geometryData,
    parallels: geometryData.parallels.filter(p => p.id !== paraId),
    points: geometryData.points.filter(p => p.id !== para.endPointId)
  };
}

/**
 * 删除常量
 */
export function deleteConstant(constantId, geometryData) {
  return {
    ...geometryData,
    constants: geometryData.constants.filter(c => c.id !== constantId)
  };
}

/**
 * 删除表达式
 */
export function deleteExpression(expressionId, geometryData) {
  return {
    ...geometryData,
    expressions: geometryData.expressions.filter(e => e.id !== expressionId)
  };
}

/**
 * 清空画布
 */
export function clearCanvas() {
  return {
    points: [],
    lines: [],
    labels: [],
    perpendiculars: [],
    parallels: [],
    constraints: [],
    constants: [],
    expressions: [],
    fills: []
  };
}

